package com.puris.dao.admin;

import com.puris.dao.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Sonata
 * @data 2021/11/26 20:25
 */
public class AllStudentDaoImpl implements AllStudentDao {
    @Override
    public StringBuffer selectStudent(Connection connection) throws SQLException {
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        StringBuffer student = new StringBuffer();

        if (connection != null) {
            String sql = "select * from student where id != ?";
            Object[] params = {"0"};

            resultSet = BaseDao.execute(connection, statement, resultSet, sql, params);

            student.append("<table>");
            student.append("<tr>");
            student.append("<th>学号</th>");
            student.append("<th>密码</th>");
            student.append("<th>系部</th>");
            student.append("<th>年级</th>");
            student.append("<th>班级</th>");
            student.append("<th>姓名</th>");
            student.append("<th>比赛</th>");
            student.append("</tr>");

            while (resultSet.next()) {
                student.append("<tr>");
                student.append("<td>").append(resultSet.getString(1)).append("</td>");
                student.append("<td>").append(resultSet.getString(2)).append("</td>");
                student.append("<td>").append(resultSet.getString(3)).append("</td>");
                student.append("<td>").append(resultSet.getString(4)).append("</td>");
                student.append("<td>").append(resultSet.getString(5)).append("</td>");
                student.append("<td>").append(resultSet.getString(6)).append("</td>");
                if (resultSet.getString(7) == null) {
                    student.append("<td></td>");
                } else {
                    student.append("<td>").append(resultSet.getString(7)).append("</td>");
                }
                student.append("</tr>");
            }

            student.append("</table>");
        }
        BaseDao.closeResource(null, statement, resultSet);

        return student;
    }
}
